package com.huaxindata.im.model.entity;

import android.os.Parcel;
import android.os.Parcelable;

import com.huaxin.common.tools.Log;

import java.util.Arrays;

public class ChatStationMessageBody extends ChatMessageBody implements Parcelable{

    protected ChatStationMessageBody(Parcel in) {
        name = in.readString();
        code = in.readString();
        address = in.readString();
        status = in.readString();
        remotePath = in.readString();
    }

    public static final Creator<ChatStationMessageBody> CREATOR = new Creator<ChatStationMessageBody>() {
        @Override
        public ChatStationMessageBody createFromParcel(Parcel in) {
            return new ChatStationMessageBody(in);
        }

        @Override
        public ChatStationMessageBody[] newArray(int size) {
            return new ChatStationMessageBody[size];
        }
    };

    @Override
    public int describeContents() {
        return 0;
    }

    @Override
    public void writeToParcel(Parcel parcel, int i) {
        parcel.writeString(name);
        parcel.writeString(code);
        parcel.writeString(address);
        parcel.writeString(status);
        parcel.writeString(remotePath);
    }

    public static class ChatStationStatus {
        /**
         * 正常
         */
        public static final String ChatStation_Normal  = 0 + "";
        /**
         * 超时
         */
        public static final String ChatStation_Timeout = 1 + "";
        /**
         * 故障
         */
        public static final String ChatStation_Error   = 2 + "";
        /**
         * 建设中
         */
        public static final String ChatStation_Build   = 3 + "";
    }

    //对应数据库的fileName
    public String name;

    public String code;

    public String address;

    public String status;
    //图片地址，为了命名统一，用remotePath
    public String remotePath;

    public ChatStationMessageBody() {
    }

    public ChatStationMessageBody(int type) {
        super(type);
    }

    public ChatStationMessageBody(int type, String code, String name, String status, String address, String image) {
        super(Type.STATION);
        this.name = name;
        this.code = code;
        this.address = address;
        this.status = status;
        this.remotePath = image;
    }

    /**
     * @param message
     * @throws NumberFormatException
     */
    public void parseStationMessage(String message) throws NumberFormatException {
        //  3#&4#&2#&19#&65#&456嘉净设备正常武汉市武昌区白沙洲大道与黄家湖路交叉口http://116.236.217.174:8080/uploads/mvg/5DU6PN/20150709090041.png
        // code.length,name.length,@"1",address.length,imageUrl.Lenth,code,name,status,address,remotePath;
        if (message == null)
            return;
        String[] words = message.split(ChatMessage.PREX);
        if (words.length < 6) {
            Log.d("Invalid key for FileMessageBody : " + Arrays.toString(words));
            return;
        }
        int clen = Integer.parseInt(words[0]);
        int nlen = Integer.parseInt(words[1]);
        int slen = Integer.parseInt(words[2]);
        int alen = Integer.parseInt(words[3]);
        String content = null;
        for (int i = 5; i < words.length; i++) {
            content = content == null ? words[i] : content + words[i];
        }
        if (content == null) {
            Log.d("Invalid Message:" + message);
            return;
        }
        code = content.substring(0, clen);
        name = content.substring(clen, clen + nlen);
        status = content.substring(clen + nlen, clen + nlen + slen);
        address = content.substring(clen + nlen + slen, clen + nlen + slen + alen);
        remotePath = content.substring(clen + nlen + slen + alen);
    }

    /**
     * 发送消息字段拼接
     * @return
     */
    public String matchStationMessage() {
        return code.length() + ChatMessage.PREX +
                name.length() + ChatMessage.PREX +
                status.length() + ChatMessage.PREX +
                address.length() + ChatMessage.PREX +
                remotePath.length() + ChatMessage.PREX +
                code + name + status + address + remotePath;
    }

    @Override
    public String toString() {
        return "ChatStationMessageBody{" +
                "name='" + name + '\'' +
                ", code='" + code + '\'' +
                ", address='" + address + '\'' +
                ", status='" + status + '\'' +
                ", remotePath='" + remotePath + '\'' +
                '}';
    }
}
